import React from 'react';

import {Button, Input, Select, Icon} from 'antd';
import ObjectList from '../../components/ObjectList';

import router from '../../../utils/routerDecorator';
import {get, post} from '../../../logic/api';

const Operation = ObjectList.Operation;
const InputEx = ObjectList.InputEx;
const SelectEx = ObjectList.SelectEx;
const FileEx = ObjectList.FileEx;

export default class Edit extends React.Component {
  
  state = {
    type:'lookbook',
    params:{},
    content:[],
    loading:false
  }

  innerIndex = 1;

  async componentWillMount(){
  }

  async componentWillReceiveProps(n){
  }

  dataIndex = [{
      type:'select',
      name:'type',
      subtitle:'类型',
      disabled:this.props.typeEnabled?false:true,
      options:[{
          value:'lookbook',
          text:'画册'
        },{
          value:'service',
          text:'服务'
        },{
          value:'class',
          text:'课程'
        },{
          value:'topic',
          text:'话题'
        },{
          value:'discover',
          text:'发现'
        }
      ]
    },{
      name:'tag',
      type:'checkbox',
      subtitle:'标签',
      options:["饮食","出行","健康","运动","起居","正装","休闲","鞋","钟表","配饰","洁面","皮具","雪茄/烟","衬衫","面料"].map((v,i)=>{
        return {
          label:v,
          value:Math.pow(2, i)
        }
      }),
    },{
      name:'kind',
      type:'radio',
      subtitle:'类别',
      options:['饮食','出行','健康','运动','起居'].map((v, i)=>{
        return {
          value:i,
          text:v
        }
      })
    },{
      name:'top',
      type:'checkbox',
      subtitle:'置顶',
      options:[{
        label:'置顶',
        value:1
      }]
    },{
      name:'etitle',
      subtitle:'英文标题',
      type:'textarea'
    },{
      name:'ctitle',
      subtitle:'中文标题',
      type:'textarea'
    },{
      name:'summary',
      subtitle:'摘要',
      type:'textarea'
    },{
      name:'headimage',
      subtitle:'首页图片',
      type:'img'
    },{
      name:'headimagesmall',
      subtitle:'首页图片缩略',
      type:'img'
    },{
      name:'params_price',
      subtitle:'服务价格'
    },{
      name:'params_time',
      subtitle:'服务时间'
    },{
      name:'params_servicetype',
      subtitle:'服务类型'
    },{
      name:'params_serviceflow',
      subtitle:'服务流程'
    },{
      name:'params_note',
      subtitle:'服务备注'
    },{
      name:'content',
      type:'gentleid_publish',
      subtitle:'内容'
    },{
      subtitle:'链接商品',
      name:'goodslinks',
      type:'cascaderchooser',
      fetchCascaderOption:async ()=>{
        var false_ret = await get('/admin/goods/allgoodslistwithcates');
        var ret = [];
        //一级分类
        false_ret.data.cates.map(v=>{
          if (v.parentId === 0){
            ret.push({
              label:v.cname,
              value:v.id,
              children:[]
            });
          }
        })

        //二级分类
        for(var i=0;i<false_ret.data.cates.length;i++){
          if (false_ret.data.cates[i].parentId !== 0){
            for(var j=0;j<ret.length;j++){
              if (ret[j].value === false_ret.data.cates[i].parentId){
                ret[j].children.push({
                  label:false_ret.data.cates[i].cname,
                  value:false_ret.data.cates[i].id,
                  children:[]
                });
                var l = ret[j].children.length;

                //三级分类
                false_ret.data.goods.map(v=>{
                  if (v.cateId === false_ret.data.cates[i].id){
                    ret[j].children[l-1].children.push({
                      label:v.cname,
                      value:v.id
                    })
                  }
                })
              }
            }
          }
        }

        return ret;
      }
    }
  ];

  

  async onOk(r, f){

    console.log("Record:", r);
    console.log("Form:", f);

    var true_tag = 0;
    f.tag.map(v=>{
      true_tag += v;
    })

    var true_top = 0;
    if (f.top[0]){
      true_top = 1;
    }

    var data = {
      id:r.id,
      etitle:f.etitle,
      ctitle:f.ctitle,
      top:true_top,
      type:f.type,
      tag:true_tag,
      kind:f.kind,
      headimage:f.headimage,
      headimagesmall:f.headimagesmall,
      summary:f.summary
    }

    if (f.type === 'service'){
      data.params = JSON.stringify({
        price:f.params_price,
        time:f.params_time,
        servicetype:f.params_servicetype,
        serviceflow:f.params_serviceflow,
        note:f.params_note
      })
    }

    if (f.content){
      data.content = JSON.stringify(f.content);
    }

    if (f.goodslinks){
      data.goodslinks = JSON.stringify(f.goodslinks);
    }

    var ret = await post('/admin/pub/upsert', data);

    if (r.id === 0 && ret.ok){
      if (this.props.reload){
        this.props.reload();
      }
    }

    return ret;
  }


  render(){
    //这里有个大问题
    //点击确定后需要重置里面的内容为新的
    //理论上应该在Operation里自动完成
    return <Operation
      width = {1200} 
      text = {this.props.text?this.props.text:("编辑发布")} 
      record = {this.props.record} 
      dataIndex = {this.dataIndex}
      fetchData = {async ()=>{
        if (this.props.fetchData){
          return await this.props.fetchData(this.props.record);
        }
        var ret = await get('/admin/pub/pubinfo/'+this.props.record.id);

        var tag = [];
        for(var i=0;i<15;i++){
          var t = Math.pow(2, i);
          if (t & ret.data.tag){
            tag.push(t);
          }
        }
        ret.data.tag = tag;

        if (ret.data.top){
          ret.data.top = [1];
        }else{
          ret.data.top = [];
        }

        if (ret.data.goodslinks){
          ret.data.goodslinks = JSON.parse(ret.data.goodslinks);
        }else{
          ret.data.goodslinks = [];
        }

        return ret;
      }}
      onOk = {async (record, formdata)=>{
        return await this.onOk(record, formdata);
      }} 
      onCancel = {async ()=>{
      }}
    />
  }

}